package com.project.slsx.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.project.slsx.R;
import com.project.slsx.bean.AddStorageBean;
import com.project.slsx.bean.StorageBean;
import com.project.slsx.utils.GlideUtils;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.List;

public class StorageInterfaceAdapter extends BaseQuickAdapter<StorageBean.ResultBean, BaseViewHolder> {
    private String amount = "0";
    private String number = "0";
    private List<AddStorageBean> addStorageBeans;
    private int spinnerPosition = 0;
    private String storage_id;
    private String in_pool = "0";
    private String gift = "0";
    private int index = 0;
    private String good_id = "";

    public StorageInterfaceAdapter(int layoutResId) {
        super(layoutResId);
        addStorageBeans = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder holder, StorageBean.ResultBean item) {
        index = holder.getLayoutPosition();
        ArrayList<String> supplier = new ArrayList<>();
        List<StorageBean.ResultBean.SupplierBean> supplier1 = item.getSupplier();
        for (int i = 0; i < supplier1.size(); i++) {
            supplier.add(supplier1.get(i).getName());
        }
        AddStorageBean addStorageBean = addStorageBeans.get(holder.getLayoutPosition());
        addStorageBean.setGoods_id(item.getGoods_id());
        addStorageBean.setTitle(item.getTitle());
        addStorageBean.setNumber(number);
        addStorageBean.setAmount(amount);
        addStorageBean.setIn_pool(in_pool);
        addStorageBean.setGifts(gift);
        addStorageBean.setSupplier_id(supplier.get(spinnerPosition));

        RequestOptions option = GlideUtils.getOption();
        ImageView ivThumb = holder.getView(R.id.iv_storage);
        NiceSpinner spinner = holder.getView(R.id.nice_spinner);
        EditText number = holder.getView(R.id.ed_bumber);
        EditText amount = holder.getView(R.id.ed_storage_amount);
        CheckBox isgift = holder.getView(R.id.cb_isgift);
        TextView addNew = holder.getView(R.id.tv_storage_add);
        number.addTextChangedListener(new NumberTextWatcher());
        amount.addTextChangedListener(new AmonutTextWatcher());
        isgift.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                gift = "1";
            } else {
                gift = "0";
            }
        });
        spinner.attachDataSource(supplier);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                spinnerPosition = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        addNew.setOnClickListener(v -> {
            Log.i(TAG, "convert: ");
        });
        Glide.with(mContext).load(item.getThumb()).apply(option).into(ivThumb);
        holder.setText(R.id.tv_storage_item_name, item.getTitle());
    }

    public void setListSize(int size) {
        for (int i = 0; i < size; i++) {
            addStorageBeans.add(new AddStorageBean());
        }
        Log.i(TAG, "setListSize: " + "add storeage bean size :" + addStorageBeans.size());
    }

    public List<AddStorageBean> getGoodsList() {
        return addStorageBeans;
    }

    private class NumberTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            number = s.toString().trim();
        }
    }

    private class AmonutTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            amount = s.toString().trim();
        }
    }

    public interface OnAddListener {
        void onAddListener();
    }

    private OnAddListener onAddListener;

    public void setOnAddListener(OnAddListener onAddListener) {
        this.onAddListener = onAddListener;
    }
}
